home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / amyboard / xboard-3.3.pl0 / xgamelist.c < prev    next >
C/C++ Source or Header  |  1995-08-12  |  12KB  |  444 lines

  1. /*
  2.  * xgamelist.c -- Game list window, part of X front end for XBoard
  3.  * $Id: xgamelist.c,v 1.4 1995/07/28 05:23:42 mann Exp $
  4.  *
  5.  * Copyright 1995 Free Software Foundation, Inc.
  6.  *
  7.  * The following terms apply to the enhanced version of XBoard distributed
  8.  * by the Free Software Foundation:
  9.  * ------------------------------------------------------------------------
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23.  * ------------------------------------------------------------------------
  24.  *
  25.  * See the file ChangeLog for a revision history.
  26.  */
  27.  
  28. #include <config.h>
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <errno.h>
  33. #include <sys/types.h>
  34.  
  35. #if STDC_HEADERS
  36. # include <stdlib.h>
  37. # include <string.h>
  38. #else /* not STDC_HEADERS */
  39. extern char *getenv();
  40. # if HAVE_STRING_H
  41. #  include <string.h>
  42. # else /* not HAVE_STRING_H */
  43. #  include <strings.h>
  44. # endif /* not HAVE_STRING_H */
  45. #endif /* not STDC_HEADERS */
  46.  
  47. #if HAVE_UNISTD_H
  48. # include <unistd.h>
  49. #endif
  50.  
  51. #include <X11/Intrinsic.h>
  52. #include <X11/StringDefs.h>
  53. #include <X11/Shell.h>
  54. #include <X11/Xaw/Dialog.h>
  55. #include <X11/Xaw/Form.h>
  56. #include <X11/Xaw/List.h>
  57. #include <X11/Xaw/Label.h>
  58. #include <X11/Xaw/SimpleMenu.h>
  59. #include <X11/Xaw/SmeBSB.h>
  60. #include <X11/Xaw/SmeLine.h>
  61. #include <X11/Xaw/Box.h>
  62. #include <X11/Xaw/MenuButton.h>
  63. #include <X11/cursorfont.h>
  64. #include <X11/Xaw/Text.h>
  65. #include <X11/Xaw/AsciiText.h>
  66. #include <X11/Xaw/Viewport.h>
  67.  
  68. #include "common.h"
  69. #include "frontend.h"
  70. #include "backend.h"
  71. #include "xboard.h"
  72. #include "xgamelist.h"
  73.  
  74. extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
  75. extern Display *xDisplay;
  76. extern int squareSize;
  77. extern Pixmap xMarkPixmap;
  78.  
  79. char gameListTranslations[] =
  80.   "<Btn1Up>(2): LoadSelectedProc() \n \
  81.    <Key>Return: LoadSelectedProc() \n";
  82.  
  83. typedef struct {
  84.     Widget shell;
  85.     Position x, y;
  86.     Dimension w, h;
  87.     Boolean up;
  88.     FILE *fp;
  89.     char *filename;
  90.     char **strings;
  91. } GameListClosure;
  92.  
  93. Widget
  94. GameListCreate(name, callback, client_data)
  95.      char *name;
  96.      XtCallbackProc callback;
  97.      XtPointer client_data;
  98. {
  99.     Arg args[16];
  100.     Widget shell, form, viewport, listwidg;
  101.     Widget b_load, b_loadprev, b_loadnext, b_close;
  102.     Dimension fw_width;
  103.     int j;
  104.     GameListClosure *glc = (GameListClosure *) client_data;
  105.  
  106.     j = 0;
  107.     XtSetArg(args[j], XtNwidth, &fw_width);  j++;
  108.     XtGetValues(formWidget, args, j);
  109.  
  110.     j = 0;
  111.     XtSetArg(args[j], XtNresizable, True);  j++;
  112.     XtSetArg(args[j], XtNallowShellResize, True);  j++;
  113. #if TOPLEVEL
  114.     shell =
  115.       XtCreatePopupShell(name, topLevelShellWidgetClass,
  116.              shellWidget, args, j);
  117. #else
  118.     shell =
  119.       XtCreatePopupShell(name, transientShellWidgetClass,
  120.              shellWidget, args, j);
  121. #endif
  122.     j = 0;
  123.     form =
  124.       XtCreateManagedWidget("form", formWidgetClass, shell, args, j);
  125.  
  126.     j = 0;
  127.     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
  128.     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
  129.     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
  130.     XtSetArg(args[j], XtNright, XtChainRight);  j++;
  131.     XtSetArg(args[j], XtNresizable, False);  j++;
  132.     XtSetArg(args[j], XtNwidth, fw_width);  j++;
  133.     XtSetArg(args[j], XtNallowVert, True); j++;
  134.     viewport =
  135.       XtCreateManagedWidget("viewport", viewportWidgetClass, form, args, j);
  136.  
  137.     j = 0;
  138.     XtSetArg(args[j], XtNlist, glc->strings);  j++;
  139.     XtSetArg(args[j], XtNdefaultColumns, 1);  j++;
  140.     XtSetArg(args[j], XtNforceColumns, True);  j++;
  141.     XtSetArg(args[j], XtNverticalList, True);  j++;
  142.     listwidg = 
  143.       XtCreateManagedWidget("list", listWidgetClass, viewport, args, j);
  144.     XawListHighlight(listwidg, 0);
  145.     XtAugmentTranslations(listwidg,
  146.               XtParseTranslationTable(gameListTranslations));
  147.  
  148.     j = 0;
  149.     XtSetArg(args[j], XtNfromVert, viewport);  j++;
  150.     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
  151.     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
  152.     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
  153.     XtSetArg(args[j], XtNright, XtChainLeft); j++;
  154.     b_load =
  155.       XtCreateManagedWidget("load", commandWidgetClass, form, args, j);
  156.     XtAddCallback(b_load, XtNcallback, callback, client_data);
  157.  
  158.     j = 0;
  159.     XtSetArg(args[j], XtNfromVert, viewport);  j++;
  160.     XtSetArg(args[j], XtNfromHoriz, b_load);  j++;
  161.     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
  162.     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
  163.     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
  164.     XtSetArg(args[j], XtNright, XtChainLeft); j++;
  165.     b_loadprev =
  166.       XtCreateManagedWidget("prev", commandWidgetClass, form, args, j);
  167.     XtAddCallback(b_loadprev, XtNcallback, callback, client_data);
  168.  
  169.     j = 0;
  170.     XtSetArg(args[j], XtNfromVert, viewport);  j++;
  171.     XtSetArg(args[j], XtNfromHoriz, b_loadprev);  j++;
  172.     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
  173.     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
  174.     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
  175.     XtSetArg(args[j], XtNright, XtChainLeft); j++;
  176.     b_loadnext =
  177.       XtCreateManagedWidget("next", commandWidgetClass, form, args, j);
  178.     XtAddCallback(b_loadnext, XtNcallback, callback, client_data);
  179.  
  180.     j = 0;
  181.     XtSetArg(args[j], XtNfromVert, viewport);  j++;
  182.     XtSetArg(args[j], XtNfromHoriz, b_loadnext);  j++;
  183.     XtSetArg(args[j], XtNtop, XtChainBottom); j++;
  184.     XtSetArg(args[j], XtNbottom, XtChainBottom); j++;
  185.     XtSetArg(args[j], XtNleft, XtChainLeft); j++;
  186.     XtSetArg(args[j], XtNright, XtChainLeft); j++;
  187.     b_close =
  188.       XtCreateManagedWidget("close", commandWidgetClass, form, args, j);
  189.     XtAddCallback(b_close, XtNcallback, callback, client_data);
  190.  
  191.     if (glc->x == -1) {
  192.     Position y1;
  193.     Dimension h1;
  194.     int xx, yy;
  195.     Window junk;
  196.  
  197.     j = 0;
  198.     XtSetArg(args[j], XtNheight, &h1); j++;
  199.     XtSetArg(args[j], XtNy, &y1); j++;
  200.     XtGetValues(boardWidget, args, j);
  201.     glc->w = fw_width * 3/4;
  202.     glc->h = squareSize * 3;
  203.  
  204.     XSync(xDisplay, False);
  205. #ifdef NOTDEF
  206.     /* This code seems to tickle an X bug if it is executed too soon
  207.        after xboard starts up.  The coordinates get transformed as if
  208.        the main window was positioned at (0, 0).
  209.     */
  210.     XtTranslateCoords(shellWidget, (fw_width - glc->w) / 2,
  211.               y1 + (h1 - glc->h + appData.borderYoffset) / 2,
  212.               &glc->x, &glc->y);
  213. #else /*!NOTDEF*/
  214.         XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
  215.                   RootWindowOfScreen(XtScreen(shellWidget)),
  216.                   (fw_width - glc->w) / 2,
  217.                   y1 + (h1 - glc->h + appData.borderYoffset) / 2,
  218.                   &xx, &yy, &junk);
  219.     glc->x = xx;
  220.     glc->y = yy;
  221. #endif /*!NOTDEF*/
  222.     }
  223.     j = 0;
  224.     XtSetArg(args[j], XtNheight, glc->h);  j++;
  225.     XtSetArg(args[j], XtNwidth, glc->w);  j++;
  226.     XtSetArg(args[j], XtNx, glc->x - appData.borderXoffset);  j++;
  227.     XtSetArg(args[j], XtNy, glc->y - appData.borderYoffset);  j++;
  228.     XtSetValues(shell, args, j);
  229.  
  230.     XtRealizeWidget(shell);
  231.  
  232.     return shell;
  233. }
  234.  
  235. void
  236. GameListCallback(w, client_data, call_data)
  237.      Widget w;
  238.      XtPointer client_data, call_data;
  239. {
  240.     String name;
  241.     Arg args[16];
  242.     int j;
  243.     Widget listwidg;
  244.     GameListClosure *glc = (GameListClosure *) client_data;
  245.     XawListReturnStruct *rs;
  246.     int index;
  247.  
  248.     j = 0;
  249.     XtSetArg(args[j], XtNlabel, &name);  j++;
  250.     XtGetValues(w, args, j);
  251.  
  252.     if (strcmp(name, "close") == 0) {
  253.     GameListPopDown();
  254.     return;
  255.     }
  256.     listwidg = XtNameToWidget(glc->shell, "form.viewport.list");
  257.     rs = XawListShowCurrent(listwidg);
  258.     if (strcmp(name, "load") == 0) {
  259.     index = rs->list_index;
  260.     if (index < 0) {
  261.         DisplayError("No game selected", 0);
  262.         return;
  263.     }
  264.     } else if (strcmp(name, "next") == 0) {
  265.     index = rs->list_index + 1;
  266.     if (index >= ((ListGame *) gameList.tailPred)->number) {
  267.         DisplayError("Can't go forward any further", 0);
  268.         return;
  269.     }
  270.     XawListHighlight(listwidg, index);
  271.     } else if (strcmp(name, "prev") == 0) {
  272.     index = rs->list_index - 1;
  273.     if (index < 0) {
  274.         DisplayError("Can't back up any further", 0);
  275.         return;
  276.     }
  277.     XawListHighlight(listwidg, index);
  278.     }
  279.     if (cmailMsgLoaded) {
  280.     CmailLoadGame(glc->fp, index + 1, glc->filename, True);
  281.     } else {
  282.     LoadGame(glc->fp, index + 1, glc->filename, True);
  283.     }
  284. }
  285.  
  286. static GameListClosure *glc = NULL;
  287.  
  288. void
  289. GameListPopUp(fp, filename)
  290.      FILE *fp;
  291.      char *filename;
  292. {
  293.     Arg args[16];
  294.     int j, nstrings;
  295.     Widget listwidg;
  296.     ListGame *lg;
  297.     char **st;
  298.  
  299.     if (glc == NULL) {
  300.     glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
  301.     glc->x = glc->y = -1;
  302.     }
  303.  
  304.     if (glc->strings != NULL) {
  305.     st = glc->strings;
  306.     while (*st) {
  307.         free(*st++);
  308.     }
  309.     free(glc->strings);
  310.     }
  311.  
  312.     nstrings = ((ListGame *) gameList.tailPred)->number;
  313.     glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
  314.     st = glc->strings;
  315.     lg = (ListGame *) gameList.head;
  316.     while (nstrings--) {
  317.     *st++ = GameListLine(lg->number, &lg->gameInfo);
  318.     lg = (ListGame *) lg->node.succ;
  319.      }
  320.     *st = NULL;
  321.  
  322.     glc->fp = fp;
  323.  
  324.     if (glc->filename != NULL) free(glc->filename);
  325.     glc->filename = StrSave(filename);
  326.  
  327.     if (glc->shell == NULL) {
  328.     glc->shell = GameListCreate(filename, GameListCallback, glc); 
  329.     } else {
  330.     listwidg = XtNameToWidget(glc->shell, "form.viewport.list");
  331.     XawListChange(listwidg, glc->strings, 0, 0, True);
  332.     XawListHighlight(listwidg, 0);
  333.     j = 0;
  334.     XtSetArg(args[j], XtNiconName, (XtArgVal) filename);  j++;
  335.     XtSetArg(args[j], XtNtitle, (XtArgVal) filename);  j++;
  336.     XtSetValues(glc->shell, args, j);
  337.     }
  338.  
  339.     XtPopup(glc->shell, XtGrabNone);
  340.     glc->up = True;
  341.     j = 0;
  342.     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
  343.     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
  344.         args, j);
  345. }
  346.  
  347. void
  348. GameListDestroy()
  349. {
  350.     if (glc == NULL) return;
  351.     GameListPopDown();
  352.     if (glc->strings != NULL) {
  353.     char **st;
  354.     st = glc->strings;
  355.     while (*st) {
  356.         free(*st++);
  357.     }
  358.     free(glc->strings);
  359.     }
  360.     free(glc);
  361.     glc = NULL;
  362. }
  363.  
  364. void
  365. ShowGameListProc(w, event, prms, nprms)
  366.      Widget w;
  367.      XEvent *event;
  368.      String *prms;
  369.      Cardinal *nprms;
  370. {
  371.     Arg args[16];
  372.     int j;
  373.  
  374.     if (glc == NULL) {
  375.     DisplayError("There is no game list", 0);
  376.     return;
  377.     }
  378.     if (glc->up) {
  379.     GameListPopDown();
  380.     return;
  381.     }
  382.     XtPopup(glc->shell, XtGrabNone);
  383.     glc->up = True;
  384.     j = 0;
  385.     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
  386.     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
  387.         args, j);
  388. }
  389.  
  390. void
  391. LoadSelectedProc(w, event, prms, nprms)
  392.      Widget w;
  393.      XEvent *event;
  394.      String *prms;
  395.      Cardinal *nprms;
  396. {
  397.     Widget listwidg;
  398.     XawListReturnStruct *rs;
  399.     int index;
  400.  
  401.     if (glc == NULL) return;
  402.     listwidg = XtNameToWidget(glc->shell, "form.viewport.list");
  403.     rs = XawListShowCurrent(listwidg);
  404.     index = rs->list_index;
  405.     if (index < 0) return;
  406.     if (cmailMsgLoaded) {
  407.     CmailLoadGame(glc->fp, index + 1, glc->filename, True);
  408.     } else {
  409.     LoadGame(glc->fp, index + 1, glc->filename, True);
  410.     }
  411. }
  412.  
  413. void
  414. GameListPopDown()
  415. {
  416.     Arg args[16];
  417.     int j;
  418.  
  419.     if (glc == NULL) return;
  420.     j = 0;
  421.     XtSetArg(args[j], XtNx, &glc->x); j++;
  422.     XtSetArg(args[j], XtNy, &glc->y); j++;
  423.     XtSetArg(args[j], XtNheight, &glc->h); j++;
  424.     XtSetArg(args[j], XtNwidth, &glc->w); j++;
  425.     XtGetValues(glc->shell, args, j);
  426.     XtPopdown(glc->shell);
  427.     XtSetKeyboardFocus(shellWidget, formWidget);
  428.     glc->up = False;
  429.     j = 0;
  430.     XtSetArg(args[j], XtNleftBitmap, None); j++;
  431.     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Game List"),
  432.         args, j);
  433. }
  434.  
  435. void
  436. GameListHighlight(index)
  437.      int index;
  438. {
  439.     Widget listwidg;
  440.     if (glc == NULL || !glc->up) return;
  441.     listwidg = XtNameToWidget(glc->shell, "form.viewport.list");
  442.     XawListHighlight(listwidg, index - 1);
  443. }
  444.